package com.ruoyi.beforehand.service.impl;

import java.util.List;

import com.ruoyi.beforehand.domain.InfoBeforehandEvaluate;
import com.ruoyi.beforehand.domain.InfoBulkAddDetailVo;
import com.ruoyi.beforehand.domain.InfoScoreVo;
import com.ruoyi.beforehand.mapper.InfoBeforehandEvaluateMapper;
import com.ruoyi.performance.domain.InfoPrePerformanceEvaluation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.beforehand.mapper.InfoBeforehandEvaluateDetailMapper;
import com.ruoyi.beforehand.domain.InfoBeforehandEvaluateDetail;
import com.ruoyi.beforehand.service.IInfoBeforehandEvaluateDetailService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 事前评估详情Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-24
 */
@Service
public class InfoBeforehandEvaluateDetailServiceImpl implements IInfoBeforehandEvaluateDetailService
{
    @Autowired
    private InfoBeforehandEvaluateDetailMapper infoBeforehandEvaluateDetailMapper;
    @Autowired
    private InfoBeforehandEvaluateMapper infoBeforehandEvaluateMapper;

    /**
     * 查询事前评估详情
     *
     * @param id 事前评估详情主键
     * @return 事前评估详情
     */
    @Override
    public InfoBeforehandEvaluateDetail selectInfoBeforehandEvaluateDetailById(Long id)
    {
        return infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailById(id);
    }

    /**
     * 查询事前评估详情列表
     *
     * @param infoBeforehandEvaluateDetail 事前评估详情
     * @return 事前评估详情
     */
    @Override
    public List<InfoBeforehandEvaluateDetail> selectInfoBeforehandEvaluateDetailList(InfoBeforehandEvaluateDetail infoBeforehandEvaluateDetail)
    {
        return infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailList(infoBeforehandEvaluateDetail);
    }

    /**
     * 新增事前评估详情
     *
     * @param infoBeforehandEvaluateDetail 事前评估详情
     * @return 结果
     */
    @Override
    public int insertInfoBeforehandEvaluateDetail(InfoBeforehandEvaluateDetail infoBeforehandEvaluateDetail)
    {
        return infoBeforehandEvaluateDetailMapper.insertInfoBeforehandEvaluateDetail(infoBeforehandEvaluateDetail);
    }

    /**
     * 修改事前评估详情
     *
     * @param infoBeforehandEvaluateDetail 事前评估详情
     * @return 结果
     */
    @Override
    public int updateInfoBeforehandEvaluateDetail(InfoBeforehandEvaluateDetail infoBeforehandEvaluateDetail)
    {
        return infoBeforehandEvaluateDetailMapper.updateInfoBeforehandEvaluateDetail(infoBeforehandEvaluateDetail);
    }

    /**
     * 批量删除事前评估详情
     *
     * @param ids 需要删除的事前评估详情主键
     * @return 结果
     */
    @Override
    public int deleteInfoBeforehandEvaluateDetailByIds(Long[] ids)
    {
        return infoBeforehandEvaluateDetailMapper.deleteInfoBeforehandEvaluateDetailByIds(ids);
    }

    /**
     * 删除事前评估详情信息
     *
     * @param id 事前评估详情主键
     * @return 结果
     */
    @Override
    public int deleteInfoBeforehandEvaluateDetailById(Long id)
    {
        return infoBeforehandEvaluateDetailMapper.deleteInfoBeforehandEvaluateDetailById(id);
    }

    @Override
    public int insertInfoBeforehandEvaluateDetailList(InfoBulkAddDetailVo infoBulkAddDetailVo) {
        try {
            List<InfoBeforehandEvaluateDetail> infoBeforehandEvaluateDetails=infoBulkAddDetailVo.getInfoBeforehandEvaluateDetails();
            for(InfoBeforehandEvaluateDetail infoBeforehandEvaluateDetail:infoBeforehandEvaluateDetails){
                infoBeforehandEvaluateDetail.setBeforehandId(infoBulkAddDetailVo.getBeforehandId());
                infoBeforehandEvaluateDetailMapper.insertInfoBeforehandEvaluateDetail(infoBeforehandEvaluateDetail);
            }
            return 1;
        }catch (Exception e) {
            System.out.println(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }

    }

    @Override
    public List<InfoBeforehandEvaluateDetail> selectInfoBeforehandEvaluateDetailsById(Long id) {
        return infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailsById(id);
    }

    @Override
    public List<InfoBeforehandEvaluateDetail> updateInfoBeforehandEvaluateDetails(InfoScoreVo infoScoreVo) {
        //获取id
        Long id=infoScoreVo.getBeforehandId();
        //查询(有没有插入过信息)
        List<InfoBeforehandEvaluateDetail> in= infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailByIds(id);
        //变
        InfoBeforehandEvaluate infoBeforehandEvaluate= infoBeforehandEvaluateMapper.selectInfoBeforehandEvaluateById(id);
        infoBeforehandEvaluate.setIsKzsqpg(1);
        infoBeforehandEvaluate.setBeforehandStatus(1);
        infoBeforehandEvaluate.setIsCr(1);
        infoBeforehandEvaluateMapper.updateInfoBeforehandEvaluate(infoBeforehandEvaluate);
        if(in.size()==0){
            //获取自评详情
            List<InfoPrePerformanceEvaluation> infoPrePerformanceEvaluation=infoScoreVo.getInfoPrePerformanceEvaluations();
            //将自评详情加到获取事前评估详情详细信息表里
            InfoBeforehandEvaluateDetail infoBeforehandEvaluateDetail=new InfoBeforehandEvaluateDetail();

            for(int i=0;i<infoPrePerformanceEvaluation.size();i++){
                BeanUtils.copyProperties(infoPrePerformanceEvaluation.get(i), infoBeforehandEvaluateDetail);
                infoBeforehandEvaluateDetail.setBeforehandId(id);
                infoBeforehandEvaluateDetail.setIsCr(1);
                infoBeforehandEvaluateDetailMapper.insertInfoBeforehandEvaluateDetail(infoBeforehandEvaluateDetail);
            }

        }
        return infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailsById(id);
    }

    @Override
    public List<InfoBeforehandEvaluateDetail> selectInfoBeforehandEvaluateDetailByIds(Long beforehandId) {

        return infoBeforehandEvaluateDetailMapper.selectInfoBeforehandEvaluateDetailByIds(beforehandId);
    }
}
